﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tetris.Data;
using System.Threading;

namespace Tetris.Contral
{
    enum TetrisType
    {
        I = 0,
        J,
        L,
        O,
        S,
        T,
        Z,
    }

    class TetrisOnMap
    {
        public TetrisOnMap()
        {
            _map = new Map();
        }

        public int MoveToDown()
        {
            int ret = 0;
            if (_map.StopToMoveDown(_td))
            {
                if (_map.SetMapData(_td))
                {
                    ret = 1;
                }
                else
                {
                    ret = 2;
                }
            }
            else
            {
                _td.MoveDown();
                ret = 0;
            }
            return ret;
        }
        
        public bool MoveToLeft()
        {
            bool ret = false;
            if (_map.StopToMoveLeft(_td))
            {
                ret = true;
            }
            else
            {
                _td.MoveLeft();
            }
            return ret;
        }

        public bool MoveToRight()
        {
            bool ret = false;
            if (_map.StopToMoveRight(_td))
            {
                ret = true;
            }
            else
            {
                _td.MoveRight();
            }
            return ret;
        }

        public bool ChangeType()
        {
            bool ret = false;
            if (_map.CheckToChange(_td))
            {
                _td.Change();
                ret = true;
            }

            return ret;
        }

        public int GetScore()
        {
            int score = 0;
            int lineNums = 0;
            for (int row = 0; row < _map.MapRows; row++)
            {
                if (_map.LineIsFull(row))
                {
                    _map.ClearLine(row);
                    lineNums++;
                    score += lineNums * 10;
                }
            }
            if (lineNums > 0)
            {
                _map.UpdateMap();
            }
            return score;
        }
                
        public TetrisData TDInMap
        {
            get
            {
                return _td;
            }
            set
            {
                _td = value;
            }
        }

        internal Map Map
        {
            get
            {
                return _map;
            }

            set
            {
                _map = value;
            }
        }

        public int Interval
        {
            get
            {
                return _interval;
            }

            set
            {
                _interval = value;
            }
        }
        private TetrisData _td;
        private Map _map;
        private int _interval;
    }
}
